/*
 * Copyright 2006 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package fr.gfi.alan.delahaye.actions.perimetre.validation;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import fr.gfi.alan.delahaye.actions.ParentStructsAction;
import fr.gfi.alan.delahaye.beans.RegularisationBean;
import fr.gfi.alan.delahaye.core.entite.Regularisation;
import fr.gfi.alan.delahaye.core.manager.PerimetreManager;

public class ValidateRegularisationPerimetreAction extends ParentStructsAction {
    
	/**
	 * 
	 */
	private static final long serialVersionUID = -8947830421648562653L;

	private Logger logger = LogManager.getLogger(ValidateRegularisationPerimetreAction.class);
	
	private RegularisationBean regularisationBean;
	
	private long idPerimetre;
	
	@Autowired
	private PerimetreManager perimetreManager;
    
    @Override
    public void validate() {
    	//TODO
    	logger.error("ValidateRegularisationPerimetreAction.validate");
    	logger.error("regularisationBean " + regularisationBean);
    	logger.error("regularisationBean.poids " + ((regularisationBean!=null)?regularisationBean.getPoids():""));
    	if(regularisationBean.getPoids() <= 0){
    		addActionError("Le nombre à régulariser est obligatoire et doit être un nombre entier et supérieur à 0.");
    	}
    	if(regularisationBean.getTypeRegularisation().equals("0")){
    		addActionError("Le type de régularisation est obligatoire..");
    	}
    	super.saveActionErrorToSession();
    }
    
    public String execute() throws Exception {
    	perimetreManager.enregistrerRegularisation(regularisationBean, idPerimetre);
    	return SUCCESS;
    }

	/**
	 * @return the regularisationBean
	 */
	public RegularisationBean getRegularisationBean() {
		return regularisationBean;
	}

	/**
	 * @param regularisationBean the regularisationBean to set
	 */
	public void setRegularisationBean(RegularisationBean regularisationBean) {
		this.regularisationBean = regularisationBean;
	}

	/**
	 * @return the idPerimetre
	 */
	public long getIdPerimetre() {
		return idPerimetre;
	}

	/**
	 * @param idPerimetre the idPerimetre to set
	 */
	public void setIdPerimetre(long idPerimetre) {
		this.idPerimetre = idPerimetre;
	}

}
